package widget;

import android.animation.ValueAnimator;
import android.app.ActionBar;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.dispatchapp.andlei.R;


/**
 * 身份弹窗
 * Created by Andlei on 2018/7/23.
 */

public class SwitchIdentityyPopupWindow extends PopupWindow {
    private Context mContext;
    private final View mView;
    private RelativeLayout mLlContent;
    private CheckBox checkbox_inshcool,checkbox_outshcool;
    private Button btn_suer;
//    private RecyclerView recyclerView;
//    private DetailAdapter adapter;
//    private List<TestBean> list = new ArrayList<>();
//    private OrderDetailBean.MoneyListBean moneyListBean;
   // private Activity mActivity;
    public SwitchIdentityyPopupWindow(Context context, View.OnClickListener onClickListener,String type) {
        super(context);
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.popupwindow_switchidentity,null);
        initView(mView);
        if(type.equals("1")){
            checkbox_inshcool.setChecked(true);
            checkbox_outshcool.setChecked(false);
        }else if(type.equals("3")){
            checkbox_inshcool.setChecked(false);
            checkbox_outshcool.setChecked(true);
        }
        btn_suer.setOnClickListener(onClickListener);
    }

    private void initView(View view) {
        mLlContent = view.findViewById(R.id.pop_layout);
        this.setContentView(mView);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setAnimationStyle(R.style.AppTheme);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(dw);

        mView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int height = mView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        hiddenPopupWindow();
                    }
                }
                return true;
            }
        });
        checkbox_inshcool =  mView.findViewById(R.id.checkbox_inshcool);
        checkbox_outshcool =  mView.findViewById(R.id.checkbox_outshcool);
        checkbox_inshcool.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        CheckBox checkBox= (CheckBox) v;
                        if(checkBox.isChecked()){
                           return true;
                        }
                        break;
                }
                return false;
            }
        });
        checkbox_outshcool.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        CheckBox checkBox= (CheckBox) v;
                        if(checkBox.isChecked()){
                            return true;
                        }
                        break;
                }
                return false;
            }
        });
        checkbox_inshcool.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.isChecked()){
                    checkbox_outshcool.setChecked(false);
                }
            }
        });
        checkbox_outshcool.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.isChecked()){
                    checkbox_inshcool.setChecked(false);
                }
            }
        });
        btn_suer = mView.findViewById(R.id.btn_suer);
//        recyclerView = mView.findViewById(R.id.list_rcy);
//        TestBean bean = new TestBean();
//        bean.str = "基础配送费";
//        bean.money = moneyListBean.getBase()+"";
//        TestBean bean1 = new TestBean();
//        bean1.str = "特殊奖励";
//        bean1.money = moneyListBean.getSpecial()+"";
//        TestBean bean2 = new TestBean();
//        bean2.str = "时段奖励";
//        bean2.money = moneyListBean.getTime()+"";
//        TestBean bean3 = new TestBean();
//        bean3.str = "等级奖励";
//        bean3.money = moneyListBean.getLevel()+"";
//        TestBean bean4 = new TestBean();
//        bean4.str = "距离奖励";
//        bean4.money = moneyListBean.getDistance()+"";
//        TestBean bean5 = new TestBean();
//        bean5.str = "任务奖励";
//        bean5.money = moneyListBean.getTask()+"";
//        list.add(bean);
//        list.add(bean1);
//        list.add(bean2);
//        list.add(bean3);
//        list.add(bean4);
//        list.add(bean5);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
//        layoutManager.setOrientation(LinearLayout.VERTICAL);
//        recyclerView.setLayoutManager(layoutManager);
//        adapter = new DetailAdapter(R.layout.item_detail,list);
//        recyclerView.setAdapter(adapter);
    }

    public void hiddenPopupWindow(){
        mView.clearAnimation();
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.slide_out);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        mView.startAnimation(animation);
        setAnimation(180,0);

    }

    private void setAnimation(int start, int end) {
        mLlContent.clearAnimation();
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);

        valueAnimator.setDuration(300).start();
        valueAnimator.setTarget(mLlContent);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                getBackground().setAlpha((Integer) animation.getAnimatedValue());
            }
        });
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        mView.clearAnimation();
        mView.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in));
        setAnimation(0, 180);
        super.showAtLocation(parent, gravity, x, y);
    }
    public int ReturnCheck(){
        int i =0;
        if(checkbox_outshcool.isChecked()){
            i=3;
        }else {
            i=1;
        }
        return i;
    }
}















